﻿using UnityEngine;
using System.Collections;
using Engine.Entities.Character;
using FrenzyIsland.Utils;

public class UICharacterItem : MonoBehaviour {

    private bool requireRefresh = false;

    public static UICharacterItem CreateOrSetComponent(GameObject gameObject, SteveBrain steveBrain,
                                                  FrenzyPeopleController peopleCtrl,
                                                  bool isSelectable = false, bool isSelected = false)
    {
        UICharacterItem component = null;

        if (gameObject.GetComponent<UICharacterItem>() == null )
            component = gameObject.AddComponent<UICharacterItem>();
        else
            component = gameObject.GetComponent<UICharacterItem>();

        component.peopleCtrl = peopleCtrl;
        component.IsSelectable = isSelectable;
        component.IsSelected = false;
        component.SteveBrain = steveBrain;

        //il characteritem add, non ha uno steve brain
        if (steveBrain!= null) gameObject.name = steveBrain.model.Name;

        component.requireRefresh = true;

        return component;
    }

    //public static UICharacterItem SetComponent(GameObject gameObject, SteveBrain steveBrain,
    //                                           FrenzyPeopleController peopleCtrl, 
    //                                           bool isSelectable = false, bool isSelected = false)
    //{
    //    //Debug.Log("SetComponent UICharacterItem: name: " + gameObject.name);

    //    UICharacterItem component = gameObject.GetComponent<UICharacterItem>();

    //    component.IsSelectable = isSelectable;
    //    component.IsSelected = false;
    //    component.SteveBrain = steveBrain;
    //    component.peopleCtrl = peopleCtrl;

    //    return component;
    //}

    public SteveBrain SteveBrain {
        get { return steveBrain; }
        set { steveBrain = value; requireRefresh = true; }
    }
    private SteveBrain steveBrain;

    public SteveModel SteveModel {
        get { return SteveBrain == null ? null : SteveBrain.model; }
    }

    #region Getter per il binding

    public string SteveName {
        get { return SteveModel == null ? "Vergingetorige" : SteveModel.Name; }
    }

    public string SteveSurname
    {
        get { return SteveModel == null ? "Vergingetorige" : SteveModel.Surname; }
    }

    public int SteveSkill_S {
        get { return SteveModel == null ? 99 : SteveModel.Skills.GetValue(SkillsEnum.Strenght); }
    }
    public int SteveSkill_A
    {
        get { return SteveModel == null ? 0 : SteveModel.Skills.GetValue(SkillsEnum.Agility); }
    }
    public int SteveSkill_C
    {
        get { return SteveModel == null ? 0 : SteveModel.Skills.GetValue(SkillsEnum.Charm); }
    }
    public int SteveSkill_R
    {
        get { return SteveModel == null ? 99 : SteveModel.Skills.GetValue(SkillsEnum.Resistance); }
    }
    public int SteveSkill_I
    {
        get { return SteveModel == null ? 0 : SteveModel.Skills.GetValue(SkillsEnum.Intelligence); }
    }
    public int Steve_Exp
    {
        get { return SteveModel == null ? 0 : SteveModel.Exp; }
    }
    public int Steve_HP
    {
        get { return SteveModel == null ? 0 : SteveModel.HP; }
    }
    public int Steve_Happiness
    {
        get { return SteveModel == null ? 0 : SteveModel.Happiness; }
    }
    public int Steve_LV
    {
        get { return SteveModel == null ? 0 : SteveModel.LV; }
    }
    public float SteveAVGSkill
    {
        get { return SteveModel == null || peopleCtrl == null ? 0.5f : peopleCtrl.GetAVGSkills(SteveModel.Skills); }
    }

    #endregion Getter per il binding

    private FrenzyPeopleController peopleCtrl;

    public UILabel lblNome;
    public UILabel lblCognome;
    public UISlider sliderAvg;
    public UISprite spriteLogo;
    public UIWidget selectedBorderContainer;
    public UIButton btnKick;

    // Proprietò che dice (e imposta) se l'oggetto è selezionato
    public bool IsSelected {
        get { return isSelected; }
        set { isSelected = value; requireRefresh = true; }
    }
    private bool isSelected;

    // non tutti gli oggetti di questo tipo posso essere selezionabili
    public bool IsSelectable { get; set; }

    public void Start()
    {
        //peopleCtrl = GameObject.FindGameObjectWithTag("VoxelWorld").GetComponent<FrenzyPeopleController>();
    }

    public void Update()
    {
        if( requireRefresh )
        {
            //imposto la selezione
            if (selectedBorderContainer)
                selectedBorderContainer.alpha = IsSelected ? 1 : 0;

            //imposto il logo
            if (SteveModel != null && spriteLogo != null)
                spriteLogo.spriteName = ResourceManager.NameSpriteGender(SteveModel.Gender);

            //se esiste il bottone kick, lo abilito solo quando l'oggetto è selezionato
            if( btnKick != null)
            {
                btnKick.gameObject.SetActive(IsSelected);
            }

            gameObject.SetActive(true);

            requireRefresh = false;
        }
    }

    #region Event

    public void Click()
    {
        //Debug.Log("Cambio la selezione - (IsSelectedEnable:" + IsSelectable);
        if ( IsSelectable )  IsSelected = !IsSelected;
    }

    #endregion Event

    #region Kick from party

    //callback all'evento kick
    public delegate void SimpleDelegate();
    private SimpleDelegate kickDelegate;

    //evento usato solo sul prefab per il partySmallDetail
    public void BtnKick_Click(UICharacterItem item)
    {
        if (peopleCtrl == null)         Debug.LogError("peopleCtrl is NULL!");
        if (item == null)               Debug.LogError("item is NULL!");
        if (item.steveBrain == null)    Debug.LogError("item.steveBrain is NULL!");

        peopleCtrl.RemoveFromParty(item.steveBrain);

        if (kickDelegate != null)
            kickDelegate();
        else
            Debug.LogError("kickDelegate is NULL");
    }

    public void SetOnKick(SimpleDelegate del)
    {
        this.kickDelegate = del;
    }

    #endregion Kick from party

}
